<?php
class NoteAction extends StudyBaseAction
{
	/**
	 * 列表
	 */
	public function noteList()
	{
		$note = M('Note');
		if(isset($_POST['title']) && $_POST['title']!=null){ 
			$where['s_note.title'] = array('like',"%{$_POST['title']}%");
		}
		if(isset($_POST['type']) && $_POST['type']!=null){ 
			$where['s_note.type'] = array('eq',"{$_POST['type']}");
		}
		import('ORG.Util.Page');
		$count = $note->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $note->field('s_note.*,u.name as name')->where($where)->join("left JOIN s_user u on s_note.user_id = u.id ")->limit($limit)->order("top desc , s_note.create_time desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('title',$_POST['title']);
		$this->assign('type',$_POST['type']);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Note:noteList');
	}
	/**
	 * 查看
	 */
	public function noteLook()
	{
		$form = M('Note');
		$where["s_note.id"] = $_GET['id'];
		$list = $form->field('s_note.*,u.name as name')->where($where)->join("left JOIN s_user u on s_note.user_id = u.id ")->find();

		$note_id["s_note_one.note_id"] = $_GET['id'];
		$note_one = M("Note_one");
		//$noteOne = $note_one->field('s_note_one.*,u.name as name')->where($note_id)->join("left JOIN s_user u on s_note_one.user_id = u.id ")->select();
		
		import('ORG.Util.Page');
		$count = $note_one->where($note_id)->count();// 查询满足要求的总记录数
		$page = new Page($count,3);
		$limit = $page->firstRow . ',' . $page->listRows;
		$noteOne = $note_one->field('s_note_one.*,u.name as name')->where($note_id)->join("left JOIN s_user u on s_note_one.user_id = u.id ")->limit($limit)->select();
		$show = $page->show();
		$this->assign('page',$show);
		
		
		$listNoteOne = array();
		foreach ($noteOne as $key=>$value)
		{
			$allrole = $this->autoWareRole($_GET['id']);
		}
		$note_two = M("Note_two");
		$note_id_two["o.note_id"] = $_GET['id'];
		$noteTwo = $note_two->field('s_note_two.*,u.name as name,uu.name as twoname')->where($note_id_two)->join("left JOIN s_user u on s_note_two.user_id = u.id left JOIN s_note_one o on s_note_two.note_one_id = o.id left JOIN s_user uu on o.user_id = uu.id  ")->select();
		
		$this->assign("p",$_GET['p']);
		$this->assign("list",$list);
		$this->assign("noteOne",$noteOne);
		$this->assign("noteTwo",$noteTwo);
		$this->assign("allrole",$allrole);
		$this->display();
	}
	public function autoWareRole($id)
	{
		$role = M("Note_one");
		$where['note_id'] = $id;
		$list = $role->field('s_note_one.*,u.name as name')->where($where)->join("left JOIN s_user u on s_note_one.user_id = u.id ")->order("s_note_one.create_time asc")->select();
		$roleMap = array();
		$i = 0;
		foreach ($list as $key=>$value)
		{
			if($value['r_id'] == 0)
			{
				$i++;
				$value["i"]=$i."楼: ";
				$roleMap[$value['id']] = $value;
			}
		}
		$this->wareRole($roleMap,$list);
		return $roleMap;
	}
	private function wareRole(&$roleMap,$roles)
	{
		foreach ($roleMap as $key=>&$value)
		{
			foreach ($roles as $k=>$v)
			{
				if($value['id'] == $v['r_id'])
				{
					if(!$value['child'])
					{
						$value['child'] = array();
					}
					$value['child'][$v['id']] = $v;
				}
			}
			$this->wareRole($value['child'],$roles);
		}
	}
	/**
	 * 置顶
	 */
	public function noteTop()
	{
		$note = M("Note");
	  	$id['id']=$_GET['id'];
	  	$data['top']='1';//1为置顶
	  	$note->where($id)->data($data)->save();
	  	$p = $_GET['p'];
		$this->redirect('Note:noteList?p='.$p);
	}
	/**
	 * 取消置顶
	 */
	public function noteNoTop()
	{
		$note = M("Note"); 
	  	$id['id']=$_GET['id'];
	  	$data['top']='0';//1为置顶,0为取消置顶
	  	$note->where($id)->data($data)->save();
	  	$p = $_GET['p'];
		$this->redirect('Note:noteList?p='.$p);
	}
	/**
	 * 删除
	 */
	public function noteDel()
	{
		$note = M("Note"); 
	  	$note_one = M('Note_one'); 
	  	$id['id']=$_GET['id'];
	  	$note_id['note_id']=$_GET['id'];
		$note_one->where($note_id)->delete(); // 删除
		$note->where($id)->delete(); // 删除
		//$_SESSION['message'] = '删除成功！';
	  	$p = $_GET['p'];
		$this->redirect('Note:noteList?p='.$p);
	}
	
}
?>